package cn.sirun.com.fca.personal.center.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import java.util.List;

import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.loader.SettingDistributorLoader;
import cn.sirun.com.fca.personal.center.model.request.DistributorRequest;
import cn.sirun.com.fca.personal.center.model.request.EditResellerRequest;
import cn.sirun.com.fca.personal.center.model.response.CityResponse;
import cn.sirun.com.fca.personal.center.model.response.DistributorResponse;
import cn.sirun.com.fca.personal.center.presenter.ISettingDistributorPresenter;
import cn.sirun.com.fca.personal.center.presenter.PersonalCenterFlowPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class SettingDistributorPresenter extends PersonalCenterFlowPresenter<SettingDistributorLoader>
        implements ISettingDistributorPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected SettingDistributorLoader createLoader() {
        return new SettingDistributorLoader();
    }

    @Override
    public void getAllCity() {
        getLoader().getAllCity()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<List<CityResponse>>>() {

                    @Override
                    public void onNext(BaseResponse<List<CityResponse>> cityResponse) {
                        super.onNext(cityResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_ALL_CITY_RESPONSE,
                                        cityResponse);
                    }
                });
    }

    @Override
    public void getCityDistributor(DistributorRequest request) {
        getLoader().getCityDistributor(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<List<DistributorResponse>>>() {

                    @Override
                    public void onNext(BaseResponse<List<DistributorResponse>> distributorResponse) {
                        super.onNext(distributorResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_ALL_DISTRIBUTOR_RESPONSE,
                                        distributorResponse);
                    }
                });
    }

    @Override
    public void editReseller(EditResellerRequest request) {
        getLoader().editReseller(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<Void>>() {

                    @Override
                    public void onNext(BaseResponse<Void> distributorResponse) {
                        super.onNext(distributorResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_EIDT_DISTRIBUTOR_RESPONSE,
                                        distributorResponse);
                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
